/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2009 - 2012 Luca Mingardi.
 *
 * This file is part of jeeObserver.
 *
 * JeeObserver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * JeeObserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package jeeobserver;

import java.io.Serializable;
import java.util.logging.Level;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;

/**
 * The Class EjbInterceptor.
 *
 * <p>
 * This inteceptor intercept ejb method invocations.<br/>
 * <br/>
 * Insert into ejb-jar.xml the code:<br/>
 * <br/>
 * <code>
 * &lt;interceptors&gt;<br/>
 * &nbsp;&lt;interceptor&gt;<br/>
 * &nbsp;&nbsp;&lt;interceptor-class&gt;jeeobserver.EjbInterceptor&lt;/interceptor-class&gt;<br/>
 * &nbsp;&lt;/interceptor&gt;<br/>
 * &lt;/interceptors&gt;<br/>
 * &lt;assembly-descriptor&gt;<br/>
 * &nbsp;&lt;interceptor-binding&gt;<br/>
 * &nbsp;&nbsp;&lt;ejb-name>*&lt;/ejb-name&gt;<br/>
 * &nbsp;&nbsp;&lt;interceptor-class&gt;jeeobserver.EjbInterceptor&lt;/interceptor-class&gt;<br/>
 * &nbsp;&lt;/interceptor-binding&gt;<br/>
 * &lt;/assembly-descriptor&gt;<br/>
 * </code>
 * </p>
 *
 * @author Luca Mingardi
 * @version 4.0
 */
public class EjbInterceptor implements Serializable {

	 /** The Constant serialVersionUID. */
	 private static final long serialVersionUID = 40L;

	 /** The project name. */
	 @Resource(name = "jeeobserverProjectName")
	 private String projectName;

	 /** The session context. */
	 @Resource
	 private SessionContext sessionContext;

	 /**
	  * Intercept.
	  *
	  * @param invocationContext the invocation context
	  *
	  * @return the object
	  *
	  * @throws Exception the exception
	  */
	 @AroundInvoke
	 public Object intercept(InvocationContext invocationContext) throws Exception {

			final String EJBName = invocationContext.getTarget().getClass().getName() + "." + invocationContext.getMethod().getName();

			String principal = null;

			try {
				 principal = this.sessionContext.getCallerPrincipal().getName();
			} catch (final Exception e) {
				 JeeObserverContext.logger.log(Level.FINE, "Unable to get EJB caller principal. ({0})", e.getMessage());
			}

			final ActionObserver observer = new ActionObserver(ActionObserver.CATEGORY_EJB, this.projectName, EJBName, principal);

			observer.start();

			try {
				 return invocationContext.proceed();
			} catch (final Exception e) {
				 observer.throwException(e);
				 throw e;
			} finally {
				 observer.stop();
			}
	 }
}
